<?php


namespace App\Models;

use Mockery\Exception;

class RemoverInfo extends Base
{

    protected $table = 'estate_remover_info';
    public $timestamps = FALSE;
    protected $appends = ['province_name','city_name','area_name','cate_name'];

    /**
     * 获取列表
     * @param null $where
     * @param string $whereRaw
     * @param string $order
     * @param string $sort
     * @param null $field
     * @param int $limit
     * @return mixed
     */
    public function getList($where = null,$whereRaw = '',$order = 'id',$sort = 'desc',$field = null,$limit = 15)
    {
        $model = self::where($where);
        if(isset($order)){
            $model = $model -> orderBy($order,$sort);
        }
        if($whereRaw){
            $model = $model ->whereRaw($whereRaw);
        }

        return $model -> select() -> paginate($limit) -> toArray();
    }


    // 添加
    public function create($data)
    {
        [$data['province'], $data['city'], $data['area']] = $data['areas'];
        $data['create_time'] = time();
        unset($data['areas']);
        return self::addInfo($data);
    }

    // 修改
    public function edit($data)
    {
        [$data['province'], $data['city'], $data['area']] = $data['areas'];
        unset($data['areas']);
        unset($data['create_time']);
        unset($data['province_name']);
        unset($data['city_name']);
        unset($data['area_name']);
        unset($data['cate_name']);
        return self::updateByWhere( ['id' => $data['id']] , $data );
    }

    // 删除
    public function deletes($ids)
    {
        $ids = explode(',',$ids);
        try {
            foreach ($ids as $key => $val){
                self::delByWhere(['id' => $val]);
            }
            return true;
        }catch (Exception $exception){
            return false;
        }
    }

    /**
     * 省份名称
     * @param $value
     * @return false|string
     */
    public function getProvinceNameAttribute()
    {
        return Area::where('id', $this->attributes['province'])->value('name');
    }
    /**
     * 城市名称
     * @param $value
     * @return false|string
     */
    public function getCityNameAttribute()
    {
        return Area::where('id', $this->attributes['city'])->value('name');
    }
    /**
     * 区县名称
     * @param $value
     * @return false|string
     */
    public function getAreaNameAttribute()
    {
        return Area::where('id', $this->attributes['area'])->value('name');
    }

    /**
     * 分类名称
     * @param $value
     * @return false|string
     */
    public function getCateNameAttribute()
    {
        return HouseCate::where('id', $this->attributes['cate_id'])->value('title');
    }

}
